package com.shopyourstyle.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.shopyourstyle.R;

public class MagItem {
	private int _x,_y, _alpha;	//Displayed position
	private int _fixedWidth;
	private int _calcHeight;
	
	private String _resName;
	private Drawable _pic;
	private Drawable _picFit, _picNotFit, _picComment;
	private String[] _textLine = new String[3];
	private static final String PACKAGE_NAME = "ShopStyle.htc" ;
	private Context _context;
	private int _height;
	private int _width;
	private Paint _textPaint = new Paint();
	private int _textHeight;
	
	private int touchedOffsetX;
	private int touchedOffsetY;
	
	private int _fit,_nfit,_comment;	//the number of FIt, NOT FIT, COMMENT

	private int fitX,notfitX,commentX;
	private int fitY,notfitY,commentY;
	private int textFitX, textNotFitX, textCommentX;
	private int textFitY, textCommentY;
	
	
	private String[] _allenString = new String[4];
	public void setAllenString(String[] allenString) {
		_allenString = allenString;
	}
	//public MagItem(Context context, String pictureName, String textLine1, String textLine2, String textLine3) {
	public MagItem(Context context, String pictureName, int fit, int nfit, int comment) {
		
		
		_context = context;
		_resName = pictureName;
		//Log.e ("MagItem", "Allocating resource : " + _resName);
		_fit = fit; _nfit = nfit; _comment = comment;
		_textLine[0] = "FIT +" + Integer.toString(_fit);
		_textLine[1] = "NOT FIT +" + Integer.toString(_nfit);
		_textLine[2] = "COMMENT +" + Integer.toString(_comment);
		_textPaint.setColor( Color.DKGRAY  );
		_textPaint.setTextSize(45);
		_textHeight = (int) ( _textPaint.getTextSize() );

		
		//_pic = this.getResources().getDrawable(R.drawable.pig);
		_picFit = _context.getResources().getDrawable(R.drawable.fit );
		_picNotFit = _context.getResources().getDrawable(R.drawable.not_fit );
		_picComment = _context.getResources().getDrawable(R.drawable.comment );
		
		
		
		int resID = _context.getResources().getIdentifier(_resName , "drawable", _context.getPackageName() );
		_pic = _context.getResources().getDrawable( resID );
		if (_pic!=null) {
			//Log.e("MagItem" , "Allocated resource [" + _resName + "]" + _pic.toString());
			_height = _pic.getIntrinsicHeight();
			_width = _pic.getIntrinsicWidth();
			//Log.e("MagItem" , "  H[" + _height + "] W[" + _width + "]" );
		}else {
			Log.e("MagItem" , "  Allocating " + _resName + " Faield" );
		}
			
		
		//getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject")
		//pic1 = this.getResources().getDrawable(R.drawable.pig);
	}
	public void setParameters(int x, int y, int alpha, int fixedWidth) {
		this._x = x;
		this._y = y;
		if (alpha < 0) 	this._alpha = 128;	//Default
		else			this._alpha = alpha;
		if (fixedWidth < 0 )	this._fixedWidth = _width;
		else					this._fixedWidth = fixedWidth;
		
		_calcHeight = (int)( (double)(_height)*((double)(_fixedWidth)/_width) ); 
		
	}
	
	public int getX() {
		return _x;
	}
	public int getY() {
		return _y;
	}
	public void dragTo(int x,int y) {
		this._x = x - touchedOffsetX; this._y = y - touchedOffsetY;
	}
	public boolean checkEvent(int localX,int localY , String tmpStr, boolean enabled) {
		//boolean firstClickEvent = false;	//For Edit Mode
		
		//Check if it is inside the Drawable ?
		if (0<= localX && localX <= _fixedWidth &&
			0<= localY && localY <= _calcHeight ) {
			//firstClickEvent = true;
			touchedOffsetX = localX;
			touchedOffsetY = localY;
			
			if (enabled) {
				Log.e ( "@checkEvent" , "Sending str:" + _allenString[0] );
				Intent myIntent = new Intent(_context, ProductsPage.class);
				myIntent.putExtra("key", _allenString[0]);
				_context.startActivity(myIntent);
			}
			
			return true;
			//[TODO] Please add necessary code here
		}
		else {
			
			if (_calcHeight <= localY && localY <= _calcHeight + _picFit .getIntrinsicHeight() ) {
				//Log.e ( "@checkEvent" , "ID:" + localX + "," + localY + "," + commentX );
				Log.e ( "@checkEvent" , "local Y is in the range" );
				if (0<= localX && localX <= 170) {
					if (enabled) {
						Log.e ( "@checkEvent" , "Enter FIT Page" );
						Intent myIntent = new Intent(_context,	FitPage.class);
						myIntent.putExtra("fitnum", this._fit );
						_context.startActivity(myIntent);
					}
					return true;
					
				}
				else if ( 170 <= localX && localX <= 220 ) {
					if (enabled)  {
						Log.e ( "@checkEvent" , "Enter Comment Page" );
						Intent myIntent = new Intent(_context, CommentPage.class);
						myIntent.putExtra("commentnum", this._comment  );
						_context.startActivity(myIntent);
					}
					return true;
				}
			}
			/*
			for (int i=0;i<3;++i) {
				int tmpX = (int)(_textPaint.measureText(this._textLine[i] ) );
				int tmpY = localY - (_calcHeight + i*this._textHeight );
				if (	0<= localX && localX <= tmpX &&
						0<= tmpY && tmpY <= this._textHeight ) {
					
					Log.e ( "@checkEvent" , _allenString[i+1] + " clicked");
					return true;
				}
			}
			*/
		}
		return false;
	}
	public void drawOnCanvas(Canvas c) {
		if (c==null) return ;
		
		//Maintain the ratio, recalculate corresponding height
		_pic.setBounds(_x, _y, _x+_fixedWidth , _y+ _calcHeight );
		_pic.setAlpha(_alpha);
		_pic.draw(c);
		
		
		
		int statusAlpha = 128;
		fitX = _x ;
		notfitX = fitX + 70;
		commentX = notfitX + 70;
		
		fitY = _y+ _calcHeight ;
		textFitY = fitY + 45;
		textCommentY = fitY + 40;
		
		//Draw FIT/ NOT FIT / Comment
		_picFit.setBounds(	fitX , fitY , 
							fitX + _picFit.getIntrinsicWidth(),
							fitY + _picFit .getIntrinsicHeight());
		_picFit.setAlpha(statusAlpha);
		_picFit.draw(c);
		
		_picNotFit.setBounds(	notfitX , fitY , 
								notfitX + _picNotFit.getIntrinsicWidth(),
								fitY + _picNotFit .getIntrinsicHeight());
		_picNotFit.setAlpha(statusAlpha);
		_picNotFit.draw(c);
		
		_picComment.setBounds(	commentX , fitY , 
								commentX + _picComment.getIntrinsicWidth(),
								fitY + _picComment .getIntrinsicHeight());
		_picComment.setAlpha(statusAlpha);
		_picComment.draw(c);


		_textPaint.setColor( Color.DKGRAY  );
		_textPaint.setTextSize(45);
		_textPaint.setAlpha(statusAlpha);
		c.drawText( Integer.toString(this._fit) ,
					fitX + 50 , textFitY , _textPaint); 

		c.drawText( Integer.toString(this._nfit) ,
				notfitX + 50 , textFitY , _textPaint);

		_textPaint.setColor( Color.WHITE    );
		_textPaint.setTextSize(30);
		_textPaint.setAlpha(statusAlpha);
		c.drawText( Integer.toString(this._comment ) ,
				commentX + 20 , textFitY - 10 , _textPaint);

		
		//Draw semi-trasparent rectangle
		/*
		c.drawRect
		paint.setAntiAlias(false);
		paint.setColor(Color.GREEN);
		canvas.drawRect(100, 5, 200, 30, paint);
		*/
		
		/*Draw Lines
		_textPaint.setAlpha(_alpha);
		for (int i=0;i<3;++i) {
			c.drawText( _textLine[i] , _x , 
						_y+ _calcHeight + _textHeight + i*(_textHeight) , _textPaint); 

		}
		//*/
		
		/*
		int textY = (int)((15 + 20 * ptr) * scale);
		c.drawText("id" + ptr + "=" + id, width - 55 * scale, textY, textPaint);
		*/
		
	}

}
